package JEE;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "JEE.Login", urlPatterns = "/JEE.Login")
public class Login extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        Cookie[] cookies = request.getCookies();
        String name = request.getParameter("name");
        String box = request.getParameter("box");
        String psd = request.getParameter("psd");
        //1.不保存
        if(box == null){
            System.out.println("box is canceled");
            if(check(name,psd)){
                session.setAttribute("name", name);
                RequestDispatcher dispatcher = request.getRequestDispatcher("buy.jsp");
                dispatcher.include(request,response);
            }else {
                RequestDispatcher dispatcher = request.getRequestDispatcher("page.html");
                dispatcher.include(request,response);
            }
        }
        //2.保存
        if(box != null){
            Cookie cookieName = null,cookiePsd = null;
            if(check(name,psd)){
                cookieName = new Cookie("name", name);
                cookieName.setMaxAge(30*24*60);
                session.setAttribute("name", name);
                response.addCookie(cookieName);

                cookiePsd = new Cookie("psd", psd);
                cookiePsd.setMaxAge(30*24*60);
                response.addCookie(cookiePsd);

                RequestDispatcher dispatcher = request.getRequestDispatcher("buy.jsp");
                dispatcher.include(request,response);
            }else {
                RequestDispatcher dispatcher = request.getRequestDispatcher("page.html");
                dispatcher.include(request,response);
            }

        }
        //3.取消保存
        if(cookies.length > 1 && box == null){
            System.out.println("取消保存");
            if(check(name,psd)){
                for(int i=0; i<cookies.length; i++){
                    if (cookies[i].getName().equals("name")) {
                        cookies[i].setMaxAge(0);
                        session.setAttribute("name", cookies[i].getValue());
                        response.addCookie(cookies[i]);
                    }
                    if (cookies[i].getName().equals("psd")) {
                        cookies[i].setMaxAge(0);
                        response.addCookie(cookies[i]);
                    }
                }
                RequestDispatcher dispatcher = request.getRequestDispatcher("buy.jsp");
                dispatcher.include(request,response);
            }
        }
    }

    private boolean check(String name, String psd) {
        String[][] users = {
                {"余健文","asd"},
                {"张皓为","123456"},
                {"visitor",""}};
        for(int i=0; i<users.length; i++) {
            System.out.println(users[i][0]+"  "+users[i][1]);
            if(name.equals(users[i][0]) && psd.equals(users[i][1])){
                return true;
            }
        }
        return false;
    }
}
